package com.wondersgroup.fjzlpt.console.dao.impl.yljgry;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgry.YljgryDAO;

@Repository
public class YljgryDAOImpl extends BaseJTDAOImpl implements YljgryDAO{
 
	@Override
	@Autowired
	@Qualifier("TB_ZD_YLJGRYXX")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	@Override
	public JSONObject queryPage(Map<String, String> map) {
		JSONObject sqlObj = this.getQueryPageSql(map);
		List list = sqlObj.getJSONArray("sqlParams");
		
		PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sqlObj.getString("sql"), "order by JGXZQHDM, YLJGDM, KSDM, YSGH", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}
	
	/**
	 * 查询所有
	 * @param map
	 * @return
	 */
	public JSONArray queryAll(Map<String, String> map){
		JSONObject sqlObj = this.getQueryPageSql(map);
		List list = sqlObj.getJSONArray("sqlParams");
		
		SQLBean bean = new SQLBean(sqlObj.getString("sql") + " order by JGXZQHDM, YLJGDM, KSDM, YSGH", list.toArray());
		return super.queryList(bean);
	}
	
	/**
	 * 分页语句拼接
	 * @param map
	 * @return
	 */
	private JSONObject getQueryPageSql(Map<String,String> map){
		JSONObject result = new JSONObject();
		
		StringBuilder sbud = new StringBuilder("select * from view_yljgryxx where 1=1 \n");
		List list = new ArrayList();
		String value = map.get("PTJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and PTJGID = ? ");
			list.add(value);
		}
		value = map.get("YLJGMC");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and YLJGMC like ? ");
			list.add("%"+value+"%");
		}
		value = map.get("XM");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and XM like ? ");
			list.add("%"+value+"%");
		}
		value = map.get("YSGH");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and YSGH = ? ");
			list.add(value);
		}
		value = map.get("JGXZQHDM");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and JGXZQHDM like ?");
			list.add(value+"%");
		}
		
		result.put("sql", sbud.toString());
		result.put("sqlParams", list);
		return result;
	}

	/**
	 * 查询医疗机构人员信息
	 * @param userId
	 * @return
	 */
	public JSONObject queryYljgryxx(String userId){
		StringBuilder sbud = new StringBuilder("select a.*, d.YLJGMC, b.GWMCDM, b.GWLBDM, b.GWBM, b.RYSFDM, b.SFBZ \n")
				.append(", c.PRACTICE_BM, c.PRACTICE_LBDM, c.PRACTICE_JBDM, c.PRACTICE_FWDM \n")
				.append(", (case c.PRACTICE_DDZY when '1' then '1' else '0' end) PRACTICE_DDZY \n")
				.append("from TB_ZD_YLJGRYXX a \n")
				.append("left join TB_ZD_YLJGXX d on a.PTJGID=d.PTJGID \n")
				.append("left join TB_ZD_YLJGRY_ZYBJ b on a.userid=b.userid \n")
				.append("left join TB_ZD_YLJGRY_PRACTICE c on a.userid=c.userid \n")
				.append("where A.USERID=?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{userId});
		return this.queryOne(sb);
	}

	/**
	 * 判断记录是否存在
	 * @return
	 */
	public boolean isExist(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("select count(1) from ")
			.append(this.tb.getTableName()).append(" where YLJGDM= ? and KSDM = ? and YSGH = ?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{map.get("YLJGDM"),map.get("KSDM"),map.get("YSGH")});
		return this.isExist(sb);
	}

	@Override
	public JSONArray queryYljgryById(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("select YSGH,XM from view_yljgryxx where 1=1 \n");
		List list = new ArrayList();
		String value = map.get("PTJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and PTJGID = ? ");
			list.add(value);
		}
		value = map.get("YLJGDM");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and YLJGDM = ? ");
			list.add(value);
		}
		SQLBean sqlBean = new SQLBean(sbud.toString());
		sqlBean.setParamAry(list);
		return this.query(sqlBean,ResultHandlerConstant.JSONARRAY);
	}

}
